//引用json数据文件
var postsData = require('../../../data/posts-data.js')
 
 //调用app全局变量
var app = getApp()

Page({
 data: {
    isPlayingMusic: false
 }
,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {

     var postId = option.id
    //  从json加载过来的数据
    this.data.currentPostId = postId
    var postData = postsData.postList[postId]
    this.setData({
      postData: postData
    })

   
   //缓存机制
    var postsCollected = wx.getStorageSync('posts_collected')
   if(postsCollected) {
     var postCollected = postsCollected[postId]
     this.setData({
       collected: postCollected
     })
   }
   else{
     var postsCollected = {};
      postsCollected[postId] = false;
     wx.setStorageSync('posts_Collected', postsCollected)
   }

      //全局变量的播放状态
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
       this.setData({
         isPlayingMusic: true
       })
   }

  this.setMusicMonitor();
  },
  

  setMusicMonitor:function() {
    //监听音乐开始事件
    var that = this
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isPlayingMusic: true
      })
      //监听全局的音乐播放状态
      app.globalData.g_isPlayingMusic =true
       // 指定当前的音乐播放
      app.globalData.g_currentMusicPostId = that.data.currentPostId
    })
    // //监听音乐暂停事件
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isPlayingMusic: false
      })
       //监听全局的音乐播放状态
      app.globalData.g_isPlayingMusic = false
      // 指定没播放的音乐为空
      app.globalData.g_currentMusicPostId = null;
    })
    
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isPlayingMusic: false
      })
      //监听全局的音乐播放状态
      app.globalData.g_isPlayingMusic = false
      // 指定没播放的音乐为空
      app.globalData.g_currentMusicPostId = null;
    })
  },

  onCollectionTap: function (e) {
     this.getPostsCollectedSyc()
     //this.getPostsCollectedAsy();
  },

     //异步方法
  // getPostsCollectedAsy: function () {
  //   var that = this;
  //   wx.getStorage({
  //     key: "posts_collected",
  //     success: function (res) {
  //       var postsCollected = res.data;
  //       var postCollected = postsCollected[that.data.currentPostId];
  //       // 收藏变成未收藏，未收藏变成收藏
  //       postCollected = !postCollected;
  //       postsCollected[that.data.currentPostId] = postCollected;
  //       that.showToast(postsCollected, postCollected);
  //     }
  //   })
  // },

  getPostsCollectedSyc: function (e) {
    var postsCollected = wx.getStorageSync('posts_collected')
    var postCollected = postsCollected[this.data.currentPostId]
    // 收藏变成未收藏，未收藏变成收藏
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected
    this.showToast(postsCollected, postCollected)
  },
  
 showToast: function (postsCollected, postCollected) {
    // 更新文章是否的缓存值
    wx.setStorageSync('posts_collected', postsCollected)
    // 更新数据绑定变量，从而实现切换图片
    this.setData({
      collected: postCollected
    }),
      wx.showToast({
        title: postCollected ? "收藏成功" : "取消成功",
        duration: 500,
        icon: "success"
      })

  },

  onShareTap:function(event) {
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到微博",
      "分享到QQ"
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor:"#405f80",
      success:function(res) {
        //res.cancel 用户是不是点击了取消按钮
        //res.tapIndex 数组元素的序号，从0开始
      wx.showModal({
        title: '用户'+ itemList[res.tapIndex],
        content: '用户是否取消?'+res.cancel+'现在无法实现分享功能',
      })
      }
    })
  },
    
  onMusicTap:function(e) {
    var currentPostId = this.data.currentPostId
    var isPlayingMusic = this.data.isPlayingMusic
    var postData = postsData.postList[currentPostId]
    if(isPlayingMusic) {
      wx.pauseBackgroundAudio()
      this.setData({
        isPlayingMusic:false
      })
    }

    else {
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImgUrl: postData.music.coverImg
      })
      this.setData({
        isPlayingMusic: true
      })
    }
    
  }
  
})